package com.huodongjia.service;

import com.huodongjia.mapper.JoinMapper;
import com.huodongjia.mapper.UserMapper;
import com.huodongjia.pojo.Join;
import com.huodongjia.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JoinService {
    @Autowired
    JoinMapper joinMapper;
    @Autowired
    UserMapper userMapper;
    public void insert(int userId, int activityId) {
        joinMapper.insert(userId,activityId);
    }

    public boolean isJoin(int userId, int activityId) {
        if (joinMapper.isJoin(userId,activityId).isEmpty())
            return false;
        else
            return true;
    }

    public void delete(int userId, int activityId) {
        joinMapper.delete(userId,activityId);
    }

    public List<User> findUser(int activityId) {
        List<Join> joins = joinMapper.findByActivityId(activityId);
        List<User> users = new ArrayList<>();
        for (Join join:joins){
            users.add(userMapper.findById(join.getJoinId()));
        }
        return users;
    }
}
